package com.cgy.service.Impl;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.cgy.entity.Xibie;
import com.cgy.repository.XibieRepository;
import com.cgy.service.XibieService;


@Service
public class XibieServiceImpl implements XibieService {
	@Autowired
	private XibieRepository XiebieR;

	@Override
	public Page<Xibie> findByxnamePage(Xibie x, Pageable pageable) {
		// TODO Auto-generated method stub
		return XiebieR.findAll(get(x), pageable);
	}
	
	private Specification<Xibie> get(Xibie x) {
		return new Specification<Xibie>() {
			
			@Override
			public Predicate toPredicate(Root<Xibie> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				// TODO Auto-generated method stub
				Predicate predicate = criteriaBuilder.conjunction();//动态sql表达式
				List<Expression<Boolean>> exList = predicate.getExpressions();
				if(x.getXid()!=null) {
					exList.add(criteriaBuilder.equal(root.get("xid"),x.getXid()));
				}
				return predicate;
			}
		};
		
	}
		
	@Transactional
	@Override
	public Xibie addAndUpdate(Xibie x) {
		// TODO Auto-generated method stub
		return XiebieR.saveAndFlush(x);
	}
	@Transactional
	@Override
	public Integer deleteXibieByID(Integer xid) {
		// TODO Auto-generated method stub
		return XiebieR.deleteXibieByID(xid);
	}

	@Override
	public List<Xibie> getAll() {
		// TODO Auto-generated method stub
		return XiebieR.findAll();
	}

}
